package org.pms.demo.webservice.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.pms.demo.webservice.exception.IMReadException;
import org.pms.demo.webservice.service.RoleVersionService;
import org.pms.demo.webservice.utils.ProjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.*;

/**
 * Created by laijianpeng on 17/10/18.
 */
@Component
@Path("v{version}/role")
public class RoleVersionRestController {

    @Autowired
    private RoleVersionService roleVersionService;
    @GET
    @Path("/view")
    @RequiresPermissions("role:view")
    public String view() {
        try{
            return ProjectUtils.getContentMsg("请求正常");
        }catch (Exception e){
            IMReadException.printf(e);
        }
        return ProjectUtils.getErrorMsg("error");
    }
    @POST
    @Path("/list")
    @RequiresPermissions("role:view")
    public String list(@FormParam("page") int page, @FormParam("page_size")int page_size) {
        try{
            return roleVersionService.getRoleList(page,page_size);
        }catch (Exception e){
            IMReadException.printf(e);
        }
        return ProjectUtils.getErrorMsg("error");
    }
    @POST
    @Path("/delete")
    @RequiresPermissions("role:delete")
    public String delete(@FormParam("id") String id) {
        try{
            return roleVersionService.deleteRoleById(id);
        }catch (Exception e){
            IMReadException.printf(e);
        }
        return ProjectUtils.getErrorMsg("error");
    }
    @POST
    @Path("/update")
    @RequiresPermissions("role:update")
    public String update(@FormParam("id") String id,@FormParam("role") String role,@FormParam("description") String description) {
        try{
            return roleVersionService.updateRole(id,role,description);
        }catch (Exception e){
            IMReadException.printf(e);
        }
        return ProjectUtils.getErrorMsg("error");
    }

}
